package handler

import (
	"chat_oa/api/request"
	"chat_oa/internal/dao"
	"chat_oa/internal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddGroup(c *gin.Context) {
	var req request.AddGroupReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	group := model.ChatGroup{
		UserId: c.GetUint("userId"),
		Name:   req.Name,
	}
	dao.Create(&group)
	if group.Id == 0 {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "群聊创建失败",
			"data":    false,
		})
		return
	}
	//保留群主身份
	result := dao.Create(&model.ChatGroupUser{
		UserId:  c.GetUint("userId"),
		GroupId: group.Id,
		Level:   1,
	})
	if !result {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "群主保留失败",
			"data":    nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code":    http.StatusOK,
		"message": "群主保留成功",
		"data":    true,
	})
}

func AddGroupUser(c *gin.Context) {
	var req request.AddGroupUserReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	//判断群聊是否存在
	var group model.ChatGroup
	dao.GetOneById(req.GroupId, &group)
	if group.Id == 0 {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "该群聊不存在",
			"data":    nil,
		})
		return
	}
	//判断user_id是否存在
	for _, userId := range req.UserIds {
		var user model.User
		dao.GetOneById(userId, &user)
		if user.Id == 0 {
			c.JSON(200, gin.H{
				"code":    10000,
				"message": "该用户不存在",
				"data":    userId,
			})
			return
		}
	}
	//判断用户是否有权限
	var groupUser model.ChatGroupUser
	dao.GetOneByFields(&model.ChatGroupUser{UserId: c.GetUint("userId"), GroupId: group.Id}, &groupUser)
	if !(groupUser.Level == 1 || groupUser.Level == 2) {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "该用户无权限",
			"data":    nil,
		})
		return
	}
	//添加用户
	for _, userId := range req.UserIds {
		var groupUser model.ChatGroupUser
		dao.GetOneByFields(&model.ChatGroupUser{UserId: userId, GroupId: group.Id}, &groupUser)
		if groupUser.Id > 0 {
			continue
		}
		dao.Create(&model.ChatGroupUser{
			UserId:  userId,
			GroupId: group.Id,
			Level:   3,
		})
	}
	c.JSON(200, gin.H{
		"code":    http.StatusOK,
		"message": "群成员添加成功",
		"data":    true,
	})
}

func SetAuth(c *gin.Context) {
	var req request.SetAuthReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	var groupUser model.ChatGroupUser
	dao.GetOneByFields(&model.ChatGroupUser{UserId: c.GetUint("userId"), GroupId: req.GroupId}, &groupUser)
	if !(groupUser.Level == 1) {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "该用户无权限修改",
			"data":    nil,
		})
		return
	}
	var group model.ChatGroupUser
	dao.GetOneByFields(&model.ChatGroupUser{UserId: req.UserId, GroupId: req.GroupId}, &group)
	if group.Id == 0 {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "该群聊无此用户",
			"data":    nil,
		})
		return
	}
	result := dao.Update(&model.ChatGroupUser{
		Id:    group.Id,
		Level: req.Level,
	})
	if !result {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "修改权限失败",
			"data":    false,
		})
		return
	}
	c.JSON(200, gin.H{
		"code":    http.StatusOK,
		"message": "修改权限成功",
		"data":    true,
	})
}

func RemoveUser(c *gin.Context) {
	var req request.RemoveUserReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	var groupUser model.ChatGroupUser
	dao.GetOneByFields(&model.ChatGroupUser{UserId: c.GetUint("userId"), GroupId: req.GroupId}, &groupUser)
	if !(groupUser.Level == 1) {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "该用户无权限踢出群成员",
			"data":    nil,
		})
		return
	}
	var group model.ChatGroupUser

	dao.GetOneByFields(&model.ChatGroupUser{UserId: req.UserId, GroupId: req.GroupId}, &group)
	if group.Id == 0 {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "该群聊不存在",
			"data":    nil,
		})
		return
	}
	result := dao.Delete(&model.ChatGroupUser{Id: group.Id})
	if !result {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "踢出群成员失败",
			"data":    false,
		})
		return
	}
	c.JSON(200, gin.H{
		"code":    http.StatusOK,
		"message": "踢出群成员成功",
		"data":    true,
	})
}

func DelGroup(c *gin.Context) {
	var req request.DelGroupReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	var group model.ChatGroup
	dao.GetOneById(req.Id, &group)
	if group.Id == 0 {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "该群聊不存在，禁止删除",
			"data":    nil,
		})
		return
	}
	if group.UserId != c.GetUint("userId") {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "只有群主有权限解散群聊",
			"data":    nil,
		})
		return
	}
	result := dao.Delete(&model.ChatGroup{Id: group.Id})
	if !result {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "解散群聊失败",
			"data":    nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code":    http.StatusOK,
		"message": "解散群聊成功",
		"data":    nil,
	})
}
